package com.example.apidemo.control;

import com.example.apidemo.entity.BaseResult;
import com.example.apidemo.entity.Item;
import com.example.apidemo.entity.User;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@RequestMapping
@RestController
//@RequestMapping("/user")
public class UserControl {
    //登录
    @RequestMapping(value = "/login1", method = RequestMethod.GET)
    public BaseResult login1(
            @RequestParam(value = "name", required = false, defaultValue = "") String name,
            @RequestParam(value = "password", required = false, defaultValue = "") String password) {
        BaseResult<User> result = new BaseResult<>();//code 0->都为空，1->用户名为空，2->密码为空,3参数都有
        if (name.isEmpty()) {
            result.setCode(1);
            result.setMsg("请输入用户名");
            if (password.isEmpty()) {
                result.setCode(0);
                result.setMsg("请输入用户名和密码");
            }
        } else {
            if (password.isEmpty()) {
                result.setCode(2);
                result.setMsg("请输入密码");
            } else {
                result.setCode(3);
                result.setData(new User(name, password));
            }
        }
        return result;
    }

    @RequestMapping(value = "/login2", method = RequestMethod.POST)
    public BaseResult login2(@Validated @RequestParam User user) {
        BaseResult<User> result = new BaseResult<>();//code 0->都为空，1->用户名为空，2->密码为空,3参数都有
        if (user.getUserName().isEmpty()) {
            result.setCode(1);
            result.setMsg("请输入用户名");
            if (user.getPassword().isEmpty()) {
                result.setCode(0);
                result.setMsg("请输入用户名和密码");
            }
        } else {
            if (user.getPassword().isEmpty()) {
                result.setCode(2);
                result.setMsg("请输入密码");
            } else {
                result.setCode(3);
                result.setData(user);
            }
        }
        return result;
    }


    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public BaseResult getList(
            @RequestParam(value = "index", required = false,defaultValue = "0") int index,
            @RequestParam(value = "pageSize", required = false,defaultValue = "0") int pageSize ) {
        BaseResult<List<Item>> result = new BaseResult<>();
        ArrayList<Item> items = new ArrayList();
        if (pageSize==0){
            pageSize = 10;
        }
        for (int i = 0; i < pageSize; i++) {
            if (i % 2 == 0) {
                items.add(new Item("","人生短短几个秋,不醉不罢休！",0, (int) (Math.random()*10000)));
            } else {
                items.add(new Item("https://img1.ujakn.com/Image/House/20191122/3Gk5LYb317p8r692vPZDVP.jpg?x-oss-process=style/b_w750_h562","世上哪有什么感同身受， 只有冷暖自知。 酸甜苦辣咸， 只有吃到嘴里才知道。 不轻易评价别人， 做好自己就行了！",1,(int) (Math.random()*10000)));
            }
        }
        result.setData(items);
        return result;
    }

    @RequestMapping(value = "/getTitles")
    public BaseResult getTitles(@RequestParam(value = "titleSize",defaultValue = "1") Integer titleSize){
        BaseResult< ArrayList<String>> result = new BaseResult();
        ArrayList<String> titles = new ArrayList();
        for (int i = 1 ;i<=titleSize ;i++){
            titles.add("Tab"+i);
        }
        result.setData(titles);

        return result;
    }

    @RequestMapping("/test")
    public String test() {
        return "Hello";
    }

}
